/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package manager.interfaces;

import java.io.FileNotFoundException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;
import manager.auxiliares.LerConfiguracoes;
import manager.auxiliares.MensagemExcessao;

/**
 *
 * @author Thiago
 */
public class DataBaseConnection {

    public Connection connection;
    private String connectionPath;
    private String user;
    private String password;
    private String version;
    private String servidor;
    private String dataBase;
    
    
    public DataBaseConnection(boolean teste) throws FileNotFoundException {
        servidor = (teste) ? "localhost" : "192.168.1.148";
        dataBase = "gerenciador_taldi";
        this.configurarParametros();

        connectionPath = "jdbc:mysql://" + servidor + "/" + dataBase;
        user = "root";
        password = "taldiengenharia";
        version = "1.0";
    }

    public void configurarParametros() {
        try {//Buscando por arquivo de configurações
            String userPath = System.getProperty("user.dir");
            LerConfiguracoes lerConfiguracoes = new LerConfiguracoes(userPath + "/lib/config.txt");
            LinkedList<LerConfiguracoes.ConfigParam> configParamList = lerConfiguracoes.processLineByLine();
            LerConfiguracoes.ConfigParam configParam;
            for (int i = 0; i < configParamList.size(); i++) {
                configParam = configParamList.get(i);
                if (configParam.getName().equalsIgnoreCase("server")) {
                    servidor = configParam.getValue();
                } else if(configParam.getName().equalsIgnoreCase("user")){
                    user = configParam.getValue();
                }
            }
        } catch (Exception e) {
        }
    }

    public void conect() {
        try {
            connection = DriverManager.getConnection(connectionPath, user, password);
        } catch (Exception e) {
            MensagemExcessao.mostrarMensagem(e, "Login");
        }
    }

    public Connection newConnection(Connection connection) throws SQLException {
        connection.close();
        connection = DriverManager.getConnection(connectionPath, user, password);
        return connection;
    }

    public Connection getConnection() {
        return connection;
    }

    public String getConnectionPath() {
        return connectionPath;
    }

    public void setConnectionPath(String connectionPath) {
        this.connectionPath = connectionPath;
    }

    public String getDataBase() {
        return dataBase;
    }

    public void setDataBase(String dataBase) {
        this.dataBase = dataBase;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getServidor() {
        return servidor;
    }

    public void setServidor(String servidor) {
        this.servidor = servidor;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }
}
